package com.ruoyi.synchdata;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.assertj.core.util.DateUtil;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.course.domain.LabCourse;
import com.ruoyi.course.domain.LabCourseTeacher;
import com.ruoyi.labdata.domain.LabOrgnization;

public class SynchDataForNY {
	private static final String targetHost = "http://10.22.224.122:8888/";//10.22.224.122 //datavapi.xxb.njupt.edu.cn

	public static List<JSONObject> getResponseJsonData(JSONObject param, String url) {
		try {
			HttpClient httpClient = HttpClientBuilder.create().build();
			;
			/*JSONObject jsonObject = new JSONObject();
			jsonObject.put("page", pageNum);
			jsonObject.put("pagesize", pageSize);
			*/
			/*if(params!=null){
				params.put("参数名", "参数值");
				jsonObject.put("params", params);
			}*/
			StringEntity entity = new StringEntity(param.toString(), "UTF-8");
			entity.setContentEncoding("UTF-8");
			entity.setContentType("application/json");
			HttpPost method = new HttpPost(targetHost + url);
			method.setEntity(entity);
			// 密钥传递applyId、secretKey
			method.setHeader("applyId", "jiangsuyidong_1619607038346");
			method.setHeader("secretKey", "9308abcd-fb80-46ef-afef-86ea709523a3");
			HttpResponse result = httpClient.execute(method);
			String retStr = EntityUtils.toString(result.getEntity());
			//System.out.println(retStr);
			JSONObject jsonData = JSONObject.parseObject(retStr);
			if((JSONObject) jsonData.get("data")==null){
				return null;
			}
			@SuppressWarnings("unchecked")
			List<JSONObject> rowsData = (List<JSONObject>) ((JSONObject) jsonData.get("data")).get("Rows");
			return rowsData;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	/**
	 * 同步机构数据.
	 */
	public static Map<String, LabOrgnization> synchDepartmentData(int pageNum, int pageSize) {
		String url = "getREST_ZZDWXX_79f015c928504eca8abecba5f6627493?";
		JSONObject param = new JSONObject();
		param.put("page", pageNum);
		param.put("pagesize", pageSize);
		List<JSONObject> rowsData = getResponseJsonData(param, url);
		Map<String, LabOrgnization> orgMap = new HashMap<String, LabOrgnization>();
		for (JSONObject item : rowsData) {
			LabOrgnization org = new LabOrgnization();
			//名称
			org.setOrgName(item.getString("mc"));
			if (item.getString("sfsy")!=null && item.getString("sfsy").equals("true")) {
				org.setOrgStatusOpt(Long.valueOf("0"));
			} else {
				org.setOrgStatusOpt(Long.valueOf("1"));
			}
			//上级代码
			org.setParentName(item.getString("ls"));
			//代码
			org.setOrgCode(item.getString("dm"));
			//  类别代码
			org.setRemark(item.getString("lbdm"));
			//排序
			org.setOrderNum(item.getString("px"));
			// 类别代码
			orgMap.put(org.getOrgCode(), org);
		}
		return orgMap;
	}

	/**
	 * 同步用户数据.
	 */
	public static List<SysUser> synchUserData(int pageNum, int pageSize) {
		String url = "getREST_E_ZXRYJCYKT_c90ec26f019744a29c86bf4a54abc6f7?";
		JSONObject param = new JSONObject();
		param.put("page", pageNum);
		param.put("pagesize", pageSize);
		List<JSONObject> rowsData = getResponseJsonData(param, url);
		List<SysUser> userList = new ArrayList<SysUser>();
		for (JSONObject item : rowsData) {
			SysUser user = new SysUser();
			// 身份类别
			user.setSflb(item.getString("sflb"));
			// 学工号
			user.setUserName(item.getString("xgh"));
			// 姓名
			user.setNickName(item.getString("xm"));
			// 物理卡号
			user.setWlkh(item.getString("wlkh"));
			// 性别
			if("女".equals(item.getString("xb"))){
				user.setSex("1");
			}else{
				user.setSex("0");
			}
			// 班号
			user.setBh(item.getString("bh"));
			// 专业
			user.setZy(item.getString("zy"));
			// 专业代码
			user.setZydm(item.getString("zydm"));
			// 是否在校
			user.setSfzx(item.getString("sfzx"));
			// 院系
			user.setYx(item.getString("yx"));
			// 现在年级
			user.setXznj(item.getString("xznj"));
			// 院系代码
			user.setYxdm(item.getString("yxdm"));
			userList.add(user);
		}
		return userList;
	}
	
	/**
	 * 同步本科生老师课表数据.
	 */
	public static List<LabCourse> synchBkCourseData(int pageNum, int pageSize, String kkxn, Integer xq) {
		String url = "getREST_FBHJXRWB_1d2b83ddd05b4280a9825d4fbf427072";
		JSONObject entity = new JSONObject();
		entity.put("page", pageNum);
		entity.put("pagesize", pageSize);
		
		JSONObject param = new JSONObject();
		if(!StringUtils.isEmpty(kkxn)){
			//学年
			param.put("xn", kkxn);
		}
		if(xq!=null){
			//学期
			param.put("xq", xq);
		}
		entity.put("params", param);
		
		//param.put("XQ", xq);
		List<JSONObject> rowsData = getResponseJsonData(entity, url);
		List<LabCourse> courseList = new ArrayList<LabCourse>();
		for (JSONObject item : rowsData) {
			LabCourse course = new LabCourse();
			//班级编号//班级名称
			course.setClassNo(item.getString("bjmc"));
			//上课地点
			course.setClassRoomId(item.getString("skdd"));
			//上课院系
			course.setCollegeOrgName(item.getString("kkxy"));
			//course.setCourseDate(courseDate);
			//课程名称
			course.setCourseName(item.getString("kcmc"));
			//课程代码
			course.setCourseNo(item.getString("kcdm"));
			//
			LabCourseTeacher tearcher = new LabCourseTeacher();
			//老师姓名
			tearcher.setTeacherName(item.getString("jsxm"));
			//教师职工号
			tearcher.setTeacherNo(item.getString("jszgh"));
			List<LabCourseTeacher> teacherList = new ArrayList<LabCourseTeacher>();
			teacherList.add(tearcher);
			course.setCourseTeacherList(teacherList);
			//
			course.setCreateBy("admin");
			course.setCreateTime(DateUtil.now());
			//专业名称
			course.setMajorName(item.getString("zymc"));
			//专业代码
			course.setMajorOpt(item.getString("zydm"));
			//课程人数 接口未提供
			String rs = item.getString("rs");
			course.setPersonNum(Integer.valueOf(StringUtils.isEmpty(rs)?"0":rs));
			//实验室名称 接口未提供
			//course.setLabName(labName);
			//开始周
			String qsz = item.getString("qsz");
			course.setWeekStart(Integer.valueOf(StringUtils.isEmpty(qsz)?"0":qsz));
			//结束周
			String jsz = item.getString("jsz");
			course.setWeekEnd(Integer.valueOf(StringUtils.isEmpty(jsz)?"0":jsz));
			//通过上课时间解析
			String sksj =  item.getString("sksj");
			course.setSksj(sksj);
			//起始节次
			//course.setSectionStart(sectionStart);
			//结束节次
			//course.setSectionEnd(sectionEnd);
			//course.setExperimentalProject("实验项目");
			courseList.add(course);
		}
		return courseList;
	}
	
	/**
	 * 同步研究生老师课表数据.
	 */
	public static List<LabCourse> synchYJSCourseData(int pageNum, int pageSize, String xn, String xq) {
		String url = "getREST_T_YJS_KKYJSKKXX_JSKB_a6d28411226e4b39bedd56b9509bf802";
		JSONObject entity = new JSONObject();
		entity.put("page", pageNum);
		entity.put("pagesize", pageSize);
		JSONObject param = new JSONObject();
		if(!StringUtils.isEmpty(xn)){
			//学年
			param.put("xn", xn);
		}
		if(!StringUtils.isEmpty(xq)){
			//学期
			param.put("kkxqdm", xq);
		}
		entity.put("params", param);
		List<JSONObject> rowsData = getResponseJsonData(entity, url);
		List<LabCourse> courseList = new ArrayList<LabCourse>();
		for (JSONObject item : rowsData) {
			LabCourse course = new LabCourse();
			//班级编号//班级名称
			course.setClassNo(item.getString("jxbh"));
			//上课地点
			course.setClassRoomId(item.getString("jsmc"));
			//上课院系
			course.setCollegeOrgName(item.getString("kkyxdm"));
			//course.setCourseDate(courseDate);
			//课程名称
			course.setCourseName(item.getString("zwmc"));
			//课程代码
			course.setCourseNo(item.getString("kcdm"));
			//
			LabCourseTeacher tearcher = new LabCourseTeacher();
			//老师姓名
			tearcher.setTeacherName(item.getString("jsxm"));
			//教师职工号
			tearcher.setTeacherNo(item.getString("gh"));
			List<LabCourseTeacher> teacherList = new ArrayList<LabCourseTeacher>();
			teacherList.add(tearcher);
			course.setCourseTeacherList(teacherList);
			//
			course.setCreateBy("admin");
			course.setCreateTime(DateUtil.now());
			//专业名称
			course.setMajorName(item.getString("zymc"));
			//专业代码
			course.setMajorOpt(item.getString("zydm"));
			//课程人数 接口未提供
			String rs = item.getString("xkrs");
			course.setPersonNum(Integer.valueOf(StringUtils.isEmpty(rs)?"0":rs));
			//实验室名称 接口未提供
			//course.setLabName(labName);
			//开始周
			String zdyzc = item.getString("zdyzc");
			course.setWeekStart(zdyzc.indexOf("1")+1);
			//结束周
			course.setWeekEnd(zdyzc.lastIndexOf("1")+1);
			//节次
			String jc = item.getString("jc");
			//开始节次
			course.setSectionStart(jc.indexOf("1")+1);
			//结束节次
			course.setSectionEnd(jc.lastIndexOf("1")+1);
			//星期
			String XQJ = item.getString("xqj");
			course.setWeekDayOpt((StringUtils.isEmpty(XQJ)?0:Long.valueOf(XQJ)));
			//course.setExperimentalProject("实验项目");
			courseList.add(course);
		}
		return courseList;
	}

}
